<template>
	<div class="baidu">
        <div id="allmap"></div>
    </div>
</template>

<script>
let address = []

export default {
	name: 'baidu',
	data() {
		return {
			
		}
	},
	created() {
      
	},
	mounted() {
		//this.initMap()
		//this.geocodeSearch()
		this.getShop()
	},
	methods: {
		//根据城市获取所在店铺
		getShop () {
			 let reqUrl = this.$store.state.reqUrl;
			 let axios = this.$http.get;
			 let _this = this;
	/*		 let address = this.address;*/
			 var geolocation = new BMap.Geolocation();
			 let latitude = ""
			 let longitude = ""
			 geolocation.getCurrentPosition(function(r){
				var adds =r.point;
				latitude = adds.lat,
				longitude  = adds.lng;	
				//console.log(latitude,longitude)
				if(this.getStatus() == BMAP_STATUS_SUCCESS){
					axios(reqUrl+'/shops/info/byCity/?ak=nrkIxPAMfG2kBsQ8d9U6THUAlyL3T3tM&location='+latitude+','+longitude, {				
					}).then(function(res) {
				      	  for(let i = 0;i<res.data.length;i++) {
				      	  	 address.push(res.data[i].address)
				      	  }
				      	  _this.currentLocation()
					})
				}else {
					alert('定位失败');
				}        
			},{enableHighAccuracy: true})//指示浏览器获取高精度的位置，默认false
		},
		
		//根据详细地址显示位置
		initMap() {
			var map = new BMap.Map("allmap");
			map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);
			// 创建地址解析器实例     
			var myGeo = new BMap.Geocoder();
			// 将地址解析结果显示在地图上，并调整地图视野    
			myGeo.getPoint(this.address, function(point) {
				if(point) {
					map.centerAndZoom(point, 16);
					map.addOverlay(new BMap.Marker(point));
				}
			}, this.address.substr(0, 2));
		},
		 //获取客户当前位置
		currentLocation() {
			//var map = new BMap.Map("allmap");
			/*var point = new BMap.Point(116.331398, 39.897445);*/
			var myCity = new BMap.LocalCity();
			myCity.get(this.myFun);
		},
		myFun(result) {
			var map = new BMap.Map("allmap");
			//map.enableScrollWheelZoom(true);
			
			var cityName = result.name;
			//map.setCenter(cityName);
			let add = address;
			
			//在地图中标记当前所在位置
		    var geolocation = new BMap.Geolocation();
		    geolocation.getCurrentPosition(function(r){
		    	//console.log(r.point)
	          if(this.getStatus() == BMAP_STATUS_SUCCESS){
	            var mk = new BMap.Marker(r.point);
	            map.addOverlay(mk);//标出所在地
	            map.panTo(r.point);//地图中心移动
	           /* alert('您的位置：'+r.point.lng+','+r.point.lat);
	            var point = new BMap.Point(r.point.lng,r.point.lat);//用所定位的经纬度查找所在地省市街道等信息
	            var gc = new BMap.Geocoder();
	            gc.getLocation(point, function(rs){
	            var addComp = rs.addressComponents; console.log(rs.address);//地址信息
	            alert(rs.address);//弹出所在地址
	          });*/
	        }else {
	            alert('failed'+this.getStatus());
	        }        
	    },{enableHighAccuracy: true})
			
			//批量标注
		    var myGeo = new BMap.Geocoder();
		    /*console.log(cityName)*/
		    map.centerAndZoom(cityName, 11);
			for(let i = 0;i<add.length;i++) {
				myGeo.getPoint(add[i], function(point){
				if (point) {
		            /* console.log(cityName)*/
					//document.getElementById("result").innerHTML +=  index + "、" + add + ":" + point.lng + "," + point.lat + "</br>";
						var address = new BMap.Point(point.lng, point.lat);
					//this.addMarker(address,new BMap.Label(index+1+":"+add,{offset:new BMap.Size(20,-10)}));
					    var marker = new BMap.Marker(address);
				        marker.setLabel(new BMap.Label(add[i],{offset:new BMap.Size(20,-10)}));
				        map.addOverlay(marker);
				}
			 }, cityName);
		    }
		},
		
		//gps
	   geocodeSearch(){
	   	/*var add = address;*/
	    var map = new BMap.Map("allmap");
	    map.enableScrollWheelZoom(true);
	    
	    //在地图中标记当前所在位置
	    var geolocation = new BMap.Geolocation();
	    geolocation.getCurrentPosition(function(r){
	    	//console.log(r.point)
          if(this.getStatus() == BMAP_STATUS_SUCCESS){
            var mk = new BMap.Marker(r.point);
            map.addOverlay(mk);//标出所在地
            map.panTo(r.point);//地图中心移动
           /* alert('您的位置：'+r.point.lng+','+r.point.lat);
            var point = new BMap.Point(r.point.lng,r.point.lat);//用所定位的经纬度查找所在地省市街道等信息
            var gc = new BMap.Geocoder();
            gc.getLocation(point, function(rs){
            var addComp = rs.addressComponents; console.log(rs.address);//地址信息
            alert(rs.address);//弹出所在地址
          });*/
        }else {
            alert('failed'+this.getStatus());
        }        
    },{enableHighAccuracy: true})
	   
	   /*//批量标注
	    var myGeo = new BMap.Geocoder();
	    map.centerAndZoom(cityName, 11);
		for(let i = 0;i<add.length;i++) {
			myGeo.getPoint(add[i], function(point){
			if (point) {
				 _this.currentLocation()
	             console.log(cityName)
				//document.getElementById("result").innerHTML +=  index + "、" + add + ":" + point.lng + "," + point.lat + "</br>";
					var address = new BMap.Point(point.lng, point.lat);
				//this.addMarker(address,new BMap.Label(index+1+":"+add,{offset:new BMap.Size(20,-10)}));
				    var marker = new BMap.Marker(address);
			        marker.setLabel(new BMap.Label(add[i],{offset:new BMap.Size(20,-10)}));
			        map.addOverlay(marker);
			}
		 }, cityName);
	    }*/
	  }
	}
}
</script>

<style scoped>
   .baidu{
   	width:100%;
   	height:500px; 
   	z-index: -44;  	
   }
   #allmap{
   	 width:100%;
   	 height: 500px;
   	 margin-top: 30px;
   }
</style>